<?php

class ConfigConfiguration extends Controller{

    private $_data;
    
    public function __construct(){
        parent::__construct();
        $this->_data = Array();
    }

    private function _render () {
        $this->_data['title'] = 'Gestión de descriptores';
        $this->_data['page'] = 'configConfigurationDescriptors';
        
        $this->_view->rendertemplate('header',$this->_data);
        $this->_view->rendertemplate('menu',$this->_data);
        $this->_view->render('config/configuration',$this->_data);
        $this->_view->rendertemplate('footer',$this->_data);
    }
    
    public function index(){
        $this->_data['page_section'] = 'main';
        $this->_render();
    }

    public function descriptors () {
        $idConfiguration = $_GET['idConfiguration'];
        if ($idConfiguration == "") {
            $this->_view->setOutput (json_encode (Array()));
            return;
        }
        
        $configurationDescriptors = $this->_model->getConfigurationDescriptors ($idConfiguration);  
        $this->_view->setOutput (json_encode ($configurationDescriptors));
    }
        
    public function addDescriptor () {
        $response = Array();

        if (!isset ($_POST['name']) || !isset ($_POST['value']) || !isset ($_POST['order']) || !isset ($_POST['idConfiguration'])) {
            $response['result'] = 'error';
            $response['message'] = 'POST_PARAMS ('.json_encode ($_POST).')';
        }
        else {
            $name = $_POST['name'];
            $value = $_POST['value'];
            $order = $_POST['order'];
            $idConfiguration = $_POST['idConfiguration'];
            
            if ($this->_model->addConfigurationDescriptor ($idConfiguration, $name, $value, $order) == true) {
                $response['result'] = 'success';
            }
            else {
                $response['result'] = 'error';
                $response['message'] = 'SQL FAIL';
            }
        }

        $this->_view->setOutput (json_encode ($response));
    }
    
    public function modifyDescriptor () {
        $response = Array();
        
        if (!isset ($_POST['id']) || !isset ($_POST['name']) || !isset ($_POST['value']) || !isset ($_POST['order'])) {
            $response['result'] = 'error';
            $response['message'] = 'POST_PARAMS ('.json_encode ($_POST).')';
        }
        else {
            $id = $_POST['id'];
            $name = $_POST['name'];
            $value = $_POST['value'];
            $order = $_POST['order'];
            
            if ($this->_model->modifyConfigurationDescriptor ($id, $name, $value, $order) == true) {
                $response['result'] = 'success';
            }
            else {
                $response['result'] = 'error';
                $response['message'] = 'SQL FAIL';
            }
        }

        $this->_view->setOutput (json_encode ($response));
    }
    
    public function removeDescriptor () {
        $response = Array();
        
        if (!isset ($_POST['id'])) {
            $response['result'] = 'error';
            $response['message'] = 'POST_PARAMS ('.json_encode ($_POST).')';
        }
        else {
            $id = $_POST['id'];
            
            if ($this->_model->removeConfigurationDescriptor ($id) == true) {
                $response['result'] = 'success';
            }
            else {
                $response['result'] = 'error';
                $response['message'] = 'SQL FAIL';
            }
        }
        
        $this->_view->setOutput (json_encode ($response));
    }

}

?>